package com.IoExercise;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class DownloadImg {
    public static void main(String[] args) {
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            URL url = new URL("http://p3.img.cctvpic.com/photoAlbum/page/performance/img/2020/4/29/1588125302844_201.png");
            is = url.openStream();
            fos = new FileOutputStream("d:/a.png");
//            byte[] buffer = new byte[10];
//            int count;
//            while ((count = is.read()) != -1){
//                fos.write(buffer,0,count);
//            }

            int buffer = -1;
            while((buffer = is.read()) != -1){
                fos.write(buffer);
            }
            System.out.println("文件拷贝成功");

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
